//
//  WithdrawViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/8.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "CommonSysParamModel.h"
#import "UIButton+CountDown.h"

#import "WithdrawViewController.h"

@interface WithdrawViewController ()

@property (nonatomic, assign) SelWithdrawType withDrawType;
@property (weak, nonatomic) IBOutlet UIButton *handleBtn;

@property (nonatomic, strong) CommonSysParamModel *model;
@end

@implementation WithdrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"提现";
    
    self.withDrawType = SelWithdrawType_wechat;
    [self fillViewWithType:self.withDrawType];
    
    self.moneyTF.text = [Utilities changeDoubleToString:self.money];
    [self request_withDrawInfo];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2];
        [self.countDownBtn pp_clipCorners:UIRectCornerAllCorners radius:self.countDownBtn.frame.size.height/2];

    });
}

- (void)fillViewWithType:(SelWithdrawType)type {
    
    [self.alipayBtn setImage:[UIImage imageNamed:@"common_nor"] forState:UIControlStateNormal];
    [self.wechchatBtn setImage:[UIImage imageNamed:@"common_nor"] forState:UIControlStateNormal];
    
    switch (type) {
        case SelWithdrawType_wechat:{
            [self.wechchatBtn setImage:[UIImage imageNamed:@"common_sel"] forState:UIControlStateNormal];
        } break;
        case SelWithdrawType_alipay:{
            [self.alipayBtn setImage:[UIImage imageNamed:@"common_sel"] forState:UIControlStateNormal];
        } break;
        default:
            break;
    }
}

- (IBAction)selWechatAction:(id)sender {
    self.withDrawType = SelWithdrawType_wechat;
    [self fillViewWithType:SelWithdrawType_wechat];
}

- (IBAction)selAlipayAction:(id)sender {
    self.withDrawType = SelWithdrawType_alipay;
    [self fillViewWithType:SelWithdrawType_alipay];
}

- (IBAction)handleAction:(id)sender {
    
    if (self.moneyTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"提现金额不能为0"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    
    [self request_withDraw];
}

- (IBAction)codeAction:(id)sender {
    [self request_withDrawSms];
}


- (void)request_withDraw {
            
    NSMutableDictionary *dic = @{}.mutableCopy;
    
    switch (self.withDrawType) {
        case SelWithdrawType_wechat:{
            [dic setValue:@(1) forKey:@"wtype"];
        } break;
        case SelWithdrawType_alipay:{
            [dic setValue:@(2) forKey:@"wtype"];
        } break;
        default:
            break;
    }
    
    [dic setValue:@(2) forKey:@"wbaseType"];
    [dic setValue:self.moneyTF.text forKey:@"wtotalAmount"];
    [dic setValue:self.codeTF.text forKey:@"vcode"];
    [dic setValue:[TTUserModel sharedObject].username forKey:@"phone"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"wbaseId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_WithDraw];

    [SVProgressHUD showWithStatus:@"正在请求提现"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
                [weakSelf.navigationController popToRootViewControllerAnimated:NO];

            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}



- (void)request_withDrawInfo {
            
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_WithDrawInfo];

    [SVProgressHUD showWithStatus:@"正在请求提现"];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                NSDictionary *resultDic = [data valueForKey:@"data"];
                weakSelf.model = [CommonSysParamModel modelWithDictionary:resultDic];

                weakSelf.descLab.text = [NSString stringWithFormat:@"*实际到账金额将扣除0.6%%手续费，此手续费为第三方平台支付收取\n*单次提现金额≥%@，每月提现次数≤%ld次",[Utilities changeDoubleToString:weakSelf.model.userMinMoney],weakSelf.model.userMonthlyFrequency];
                
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

- (void)request_withDrawSms {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].username forKey:@"mobile"];
    [dic setValue:@"6" forKey:@"type"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_SMS];

    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        [SVProgressHUD dismiss];

        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                [weakSelf.countDownBtn startWithTime:59
                                           title:@"重新获取"
                                  countDownTitle:@"s"
                                       mainColor:UIColorHex(0xFDB44B)
                                      countColor:UIColorHex(0xFDB44B)];
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {

        [SVProgressHUD showErrorWithStatus:@"请求异常"];
        [SVProgressHUD dismissWithDelay:1];

    }];
}

@end
